﻿// <copyright file="AppNotificationTestSender.cs" company="Robert Venables">
// Copyright (c) 2009 Robert Venables.
// </copyright>
// <author>Robert Venables</author>
// <email>rob@rvenables.com</email>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VNotify;

namespace VNotifyTest.Mock
{
    /// <summary>
    /// A test AppNotification sender. Runs assertions and outputs debug
    /// information instead of actually sending notifications.
    /// </summary>
    internal class AppNotificationTestSender : IAppNotificationSender
    {
        /// <summary>
        /// The API key that was supplied during initialization.
        /// </summary>
        private string apiKey = string.Empty;

        /// <summary>
        /// Gets or sets the total number of notifications that the test sender class
        /// has sent since being initialized or last reset.
        /// </summary>
        public static int SendCount { get; internal set; }

        /// <summary>
        /// Gets or sets an exception to throw during the simulated send process.
        /// </summary>
        public Exception SimulatedException { get; set; }

        /// <summary>
        /// Gets a value indicating whether the notification has
        /// been sent or not.
        /// </summary>
        public bool SentNotification { get; private set; }

        /// <summary>
        /// Gets a value indicating whether this test sender has been initialized yet.
        /// </summary>
        public bool Initialized { get; private set; }

        /// <summary>
        /// Gets the last sent notification.
        /// </summary>
        public AppNotification LastSentNotification { get; private set; }

        /// <summary>
        /// Gets the API key used to initialize this test sender.
        /// </summary>
        string IAppNotificationSender.ApiKey
        {
            get { return this.apiKey; }
        }

        /// <summary>
        /// Initializes the test sender with the stipulated API key.
        /// </summary>
        /// <param name="apiKey">The test api key to initialize with.</param>
        public void Initialize(string apiKey)
        {
            Debug.WriteLine("AppNotificationTestSender: Initialize()"); 

            this.apiKey = apiKey;
            Assert.IsFalse(string.IsNullOrEmpty(this.apiKey));

            this.Initialized = true;
        }

        /// <summary>
        /// Asserts that the source notification is well formed, the sender
        /// has been initialized properly, and the notification is ready to be sent.
        /// Simulates sending the notification by sleeping for a small time.
        /// </summary>
        /// <param name="source">The source notification that is intended to be sent.</param>
        public void SendNotification(AppNotification source)
        {
            Debug.WriteLine("AppNotificationTestSender: SendNotification()");

            Assert.IsNotNull(source); // Supplied Notification?
            Assert.IsTrue(this.Initialized); // Already Initialized?
            Assert.IsFalse(string.IsNullOrEmpty(this.apiKey)); // API Key Exists?
            Assert.IsFalse(string.IsNullOrEmpty(source.Message)); // Message Exists?

            // Simulate sending - useful for testing async code
            System.Threading.Thread.Sleep(250);

            // Throw simulated exception (if provided)
            if (this.SimulatedException != null)
            {
                throw this.SimulatedException;
            }

            this.LastSentNotification = source;
            SendCount++;

            this.SentNotification = true;
        }
    }
}